package com.bac.bacplatform;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import com.bac.utils.Util;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class FindPickActivity extends BaseActivity implements OnClickListener {
	private ViewPager mViewPager;
	// 广告栏图片
	private ArrayList<Integer> mImages;

	// 广告栏小点
	private List<View> mDotsList;

	private AtomicInteger atomicInteger = new AtomicInteger(0);

	private ScheduledExecutorService scheduledExecutorService;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_find_pick);
		initViewPager();
	}

	@Override
	public void init() {
		findViewById(R.id.findpick_ok_btn).setOnClickListener(this);
		findViewById(R.id.title_left).setOnClickListener(this);
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.findpick_ok_btn:
			Intent intentToOpen = new Intent(this, GasTreasureActivity.class);
			startActivityIn(intentToOpen);
			break;
		case R.id.title_left:
			finishActivityOut();
			break;
		default:
			break;
		}
	}
	@Override
	public void onStart() {
		scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
		scheduledExecutorService.scheduleAtFixedRate(new ScrollTask(), 2, 5,
				TimeUnit.SECONDS);
		System.gc();
		super.onStart();
	}

	@Override
	public void onStop() {
		// 当Activity不可见的时候停止切换
		scheduledExecutorService.shutdown();
		super.onStop();
	}

	/**
	 * 广告图片切换任务
	 * 
	 * @author Administrator
	 * 
	 */
	private class ScrollTask implements Runnable {

		public void run() {
			synchronized (mViewPager) {
				atomicOption();
				viewHandler.sendEmptyMessage(atomicInteger.get());
			}
		}

	}

	private void atomicOption() {
		atomicInteger.incrementAndGet();
		if (atomicInteger.get() > mImages.size() - 1) {
			atomicInteger.getAndAdd(-mImages.size());
		}
	}

	private void initViewPager() {
		// 从布局文件中获取ViewPager父容器
		LinearLayout pagerLayout = (LinearLayout) this
				.findViewById(R.id.find_pick_pager_content);
		// 创建ViewPager
		mViewPager = new ViewPager(FindPickActivity.this);

		// 自适应高度
		int picHeight = Util.getAdjustImageHeigh(getResources(),
				R.drawable.findpick_buddy1, FindPickActivity.this);

		// 根据图片高度设置广告栏高度
		mViewPager.setLayoutParams(new LayoutParams(
				Util.getWidth(FindPickActivity.this), picHeight));

		// 将ViewPager容器设置到布局文件父容器中
		pagerLayout.addView(mViewPager);

		initPageAdapter();

		initCirclePoint();

		mViewPager.setOnPageChangeListener(new AdPageChangeListener());
	}

	/**
	 * 切换当前显示的图片
	 */
	@SuppressLint("HandlerLeak")
	private final Handler viewHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			mViewPager.setCurrentItem(msg.what);
			super.handleMessage(msg);
		}

	};

	private void initPageAdapter() {
		mImages = new ArrayList<Integer>();
		// mImages.add(R.drawable.home_bg_banner);
		mImages.add(R.drawable.findpick_buddy1);
		mImages.add(R.drawable.findpick_buddy2);
		mImages.add(R.drawable.findpick_buddy3);
		mViewPager.setAdapter(new BannerAdapter());
	}

	private void initCirclePoint() {

		LinearLayout group = (LinearLayout) this.findViewById(R.id.find_pick_viewGroup);

		mDotsList = new ArrayList<View>();

		try {
			group.removeAllViews();
		} catch (Exception ex) {

		}

		LinearLayout.LayoutParams pl = new LinearLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		pl.setMargins(5, 0, 5, 0);
		// 广告栏的小圆点图标
		for (int i = 0; i < mImages.size(); i++) {
			LinearLayout lay = new LinearLayout(FindPickActivity.this);
			lay.setLayoutParams(pl);
			// 初始值, 默认第0个选中
			if (i == 0) {
				lay.setBackgroundResource(R.drawable.find_pink_dian_on);
			} else {
				lay.setBackgroundResource(R.drawable.find_pink_dian_off);
			}

			// 将小圆点放入到布局中
			group.addView(lay);
			mDotsList.add(lay);
		}
	}

	/**
	 * ViewPager 页面改变监听器
	 */
	private final class AdPageChangeListener implements OnPageChangeListener {
		private int oldPosition = 0;

		/**
		 * 页面滚动状态发生改变的时候触发
		 */
		@Override
		public void onPageScrollStateChanged(int arg0) {
		}

		/**
		 * 页面滚动的时候触发
		 */
		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		/**
		 * 页面选中的时候触发
		 */
		@Override
		public void onPageSelected(int arg0) {
			// 获取当前显示的页面是哪个页面
			atomicInteger.getAndSet(arg0);
			mDotsList.get(oldPosition).setBackgroundResource(
					R.drawable.find_pink_dian_off);
			mDotsList.get(arg0).setBackgroundResource(
					R.drawable.find_pink_dian_on);
			oldPosition = arg0;
		}
	}

	private final class BannerAdapter extends PagerAdapter {

		/**
		 * 从ViewPager中删除集合中对应索引的View对象
		 */
		@Override
		public void destroyItem(View container, int position, Object object) {
			((ViewPager) container).removeView((View) object);
		}

		/**
		 * 获取ViewPager的个数
		 */
		@Override
		public int getCount() {
			return mImages.size();
		}

		/**
		 * 从View集合中获取对应索引的元素, 并添加到ViewPager中
		 */
		@Override
		public Object instantiateItem(View container, int position) {
			RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
					LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
			ImageView imageView = new ImageView(FindPickActivity.this);
			imageView.setLayoutParams(params);
			imageView.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					// TODO 添加点击事件
				}
			});
			imageView.setBackgroundResource(mImages.get(position));
			try {
				((ViewPager) container).addView(imageView);
			} catch (Exception ex) {

			}
			return imageView;
		}

		/**
		 * 是否将显示的ViewPager页面与instantiateItem返回的对象进行关联 这个方法是必须实现的
		 */
		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view == object;
		}
	}
}
